package com.qianfeng.zhangwei.xutils.recycler;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.qianfeng.zhangwei.xutils.R;
import com.qianfeng.zhangwei.xutils.recycler.adapter.StaggerBaseAdapter;
import com.qianfeng.zhangwei.xutils.recycler.bean.Entry;
import com.qianfeng.zhangwei.xutils.recycler.bean.Good;

import java.util.ArrayList;
import java.util.List;


public class StraggerFragment extends Fragment implements StaggerBaseAdapter.OnItemClickLinstener {
    private String url = "http://mobapi.meilishuo.com/2.0/twitter/fashion?offset=0&limit=30&trace=popular_2015-08-04_180&imei=000000000000000&mac=08%3A00%3A27%3Aed%3Aae%3A5e&qudaoid=11601&access_token=13296268c8673a69624caac8a2e855d6&device_id=mac_08%3A00%3A27%3Aed%3Aae%3A5e&st=1438759432&_sign=e5b201bac08791be38f6f97ba7df2cf83fb08425";

    @ViewInject(R.id.fragment_stagger_recycler)
    private RecyclerView recyclerView;
    StaggerBaseAdapter adapter;
    private List<Good> goods = new ArrayList<>();
    private HttpHandler<String> httpHandler;

    public StraggerFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_stragger, container, false);
        ViewUtils.inject(this, view);
        initView();
        loadData();
        return view;
    }


    private void initView() {
        recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
        /**
         * 参数说明
         * 1  上下文
         * 2  滑动方向
         * 3  如果是true 从上至下滑动, false 相反
         */
//        LinearLayoutManager manager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, true);
//        StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(),3, LinearLayoutManager.VERTICAL, false);

        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        adapter = new StaggerBaseAdapter(getActivity(), goods);
        adapter.setOnItemClickLinstener(this);
        recyclerView.setAdapter(adapter);
    }

    private void loadData() {
        HttpUtils httpUtils = new HttpUtils();

        httpHandler = httpUtils.send(HttpRequest.HttpMethod.GET, url, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Entry entry = JSONObject.parseObject(responseInfo.result, Entry.class);
                goods.addAll(entry.getData().getList());
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });


    }

    @Override
    public void onItemClickLinstener(int position) {

    }


    @Override
    public void onStop() {
        super.onStop();
        httpHandler.cancel();
    }
}
